package webmarket;

/**
 * @author Petter Sellers ( USER_PROFILE )
 */

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;
import model.Customer;

public class UserEditProfile extends javax.swing.JFrame {

    /**
     * Creates new form UserEditProfile
     */

    private static EntityManager em;
    private javax.persistence.Query query1;
    private Integer customerId;
    private String creditbank;

    Customer logedUser;
    
    public UserEditProfile() {
         EntityManagerFactory emf = Persistence.createEntityManagerFactory("WebMarketPU");
         em = emf.createEntityManager();        
         initComponents();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        DeleteButton = new javax.swing.JButton();
        screenAddress = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        screenEmail = new javax.swing.JTextField();
        screenLastName = new javax.swing.JTextField();
        screenFirstName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        screenCreditCardNumber = new javax.swing.JTextField();
        screenCreditCardOwnerName = new javax.swing.JTextField();
        screenCreditCardCvv = new javax.swing.JTextField();
        screenCreditCardBank = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        screenPointsCardNumber = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        OkButton = new javax.swing.JButton();
        ExitButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("WebMarket - Διαχείριση Προφίλ");

        DeleteButton.setText("Διαγραφή");
        DeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Όνομα");

        jLabel4.setText("email");

        jLabel3.setText("Επώνυμο");

        screenEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                screenEmailActionPerformed(evt);
            }
        });

        screenLastName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                screenLastNameActionPerformed(evt);
            }
        });

        screenFirstName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                screenFirstNameActionPerformed(evt);
            }
        });

        jLabel5.setText("Διεύθυνση");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Στοιχεία πιστωτικής κάρτας", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));

        jLabel7.setText("Αριθμός κάρτας");

        jLabel8.setText("Όνομα κατόχου");

        jLabel9.setText("CVV");

        jLabel10.setText("Τράπεζα");

        screenCreditCardNumber.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                screenCreditCardNumberActionPerformed(evt);
            }
        });

        screenCreditCardBank.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "NBG", "EUROBANK", "ALPHA", "CITIBANK", "ΠΕΙΡΑΙΩΣ", "ΚΥΠΡΟΥ" }));
        screenCreditCardBank.setToolTipText("");
        screenCreditCardBank.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                screenCreditCardBankActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(screenCreditCardCvv, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(screenCreditCardNumber, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                            .addComponent(screenCreditCardOwnerName))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(screenCreditCardBank, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(screenCreditCardNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(screenCreditCardOwnerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(screenCreditCardCvv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(screenCreditCardBank, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        jLabel6.setText("Αρ. κάρτας πόντων ");

        screenPointsCardNumber.setEnabled(false);
        screenPointsCardNumber.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                screenPointsCardNumberActionPerformed(evt);
            }
        });

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel11.setText("Eπεξεργασία Προφίλ");

        OkButton.setText("Αποθήκευση");
        OkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OkButtonActionPerformed(evt);
            }
        });

        ExitButton1.setText("Ακύρωση/Επιστροφή");
        ExitButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(ExitButton1)
                .addGap(140, 140, 140))
            .addGroup(layout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addComponent(OkButton, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(DeleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(screenAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                                    .addComponent(screenEmail)
                                    .addComponent(screenLastName)
                                    .addComponent(screenFirstName)
                                    .addComponent(screenPointsCardNumber)))
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(134, 134, 134)
                        .addComponent(jLabel11)))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(screenFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(screenLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(screenEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(screenAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(screenPointsCardNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DeleteButton)
                    .addComponent(OkButton))
                .addGap(18, 18, 18)
                .addComponent(ExitButton1)
                .addGap(40, 40, 40))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteButtonActionPerformed
      
        int holdExit = JOptionPane.showConfirmDialog(this, "Σίγουρα ζητήσατε διαγραφή...     ", "     Προσοχή !", JOptionPane.YES_NO_CANCEL_OPTION);

        if (holdExit != 0)
        return;        
        
        
        Customer temp = em.merge(logedUser);
        
        
        try {
            em.getTransaction().begin();
            em.remove(temp);
            em.getTransaction().commit(); 
            } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
            }
        
        
        JOptionPane.showMessageDialog(rootPane ,"Το Προφίλ σας διαγράφηκε, σας ευχαριστούμε για την εμπιστοσύνη σας...     " ,"     WebMarket",1);
        
        System.exit(0);
               
    }//GEN-LAST:event_DeleteButtonActionPerformed

    private void screenFirstNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_screenFirstNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_screenFirstNameActionPerformed

    private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OkButtonActionPerformed
    
        String theFirstName           = new String(screenFirstName.getText());   
        String theLastName            = new String(screenLastName.getText());   
        String theEmail               = new String(screenEmail.getText());   
        String theAddress             = new String(screenAddress.getText());   
        String thePointsCardNumber    = new String(screenPointsCardNumber.getText());   
        String theCreditCardNumber    = new String(screenCreditCardNumber.getText());   
        String theCreditCardOwnerName = new String(screenCreditCardOwnerName.getText());   
        String theCreditCardCvv       = new String(screenCreditCardCvv.getText());   
      
        creditbank = screenCreditCardBank.getSelectedItem().toString();
        
        logedUser.setFirstName(theFirstName);
        logedUser.setLastName(theLastName);               
        logedUser.setEmail(theEmail);     
        logedUser.setAddress(theAddress);  
        logedUser.setPointsCardNumber(thePointsCardNumber); 
        logedUser.setCreditCardNumber(theCreditCardNumber); 
        logedUser.setCreditCardOwnerName(theCreditCardOwnerName); 
        logedUser.setCreditCardCvv(theCreditCardCvv); 
        logedUser.setCreditCardBank(creditbank); 
        
             
        Customer temp = em.merge(logedUser);
        em.persist(temp);
        em.getTransaction().begin();
        em.getTransaction().commit();        
                
          
        JOptionPane.showMessageDialog(rootPane ,"Το Προφίλ σας ενημερώθηκε...     " ,"     WebMarket",1);


        UserProfile userprofile = new UserProfile();
        userprofile.setLogedUser(logedUser);
        userprofile.setLocationRelativeTo(null);
        userprofile.setVisible(true);
        dispose();
        
    }//GEN-LAST:event_OkButtonActionPerformed

    private void ExitButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitButton1ActionPerformed

        int holdExit = JOptionPane.showConfirmDialog(this, "Σίγουρα ζητήσατε ακύρωση & επιστροφή...     ", "     WebMarket", JOptionPane.YES_NO_CANCEL_OPTION);

        if (holdExit != 0)
        return;

        UserProfile userprofile = new UserProfile();
        userprofile.setLogedUser(logedUser);
        userprofile.setLocationRelativeTo(null);
        userprofile.setVisible(true);
        dispose();
    }//GEN-LAST:event_ExitButton1ActionPerformed

    private void screenLastNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_screenLastNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_screenLastNameActionPerformed

    private void screenEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_screenEmailActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_screenEmailActionPerformed

    private void screenCreditCardNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_screenCreditCardNumberActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_screenCreditCardNumberActionPerformed

    private void screenPointsCardNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_screenPointsCardNumberActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_screenPointsCardNumberActionPerformed

    private void screenCreditCardBankActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_screenCreditCardBankActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_screenCreditCardBankActionPerformed

    public void setLogedUser (Customer logedUser) {
        this.logedUser = logedUser;
                      
        screenFirstName.setText(logedUser.getFirstName());                                                   
        screenLastName.setText(logedUser.getLastName());                                                     
        screenEmail.setText(logedUser.getEmail());                                                           
        screenAddress.setText(logedUser.getAddress());                                                       
        screenPointsCardNumber.setText(logedUser.getPointsCardNumber());                                     
        screenCreditCardNumber.setText(logedUser.getCreditCardNumber());                                     
        screenCreditCardOwnerName.setText(logedUser.getCreditCardOwnerName());                               
        screenCreditCardCvv.setText(logedUser.getCreditCardCvv());  
            
        screenCreditCardBank.setSelectedItem(logedUser.getCreditCardBank()); 
        
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UserEditProfile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UserEditProfile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UserEditProfile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UserEditProfile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UserEditProfile().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton DeleteButton;
    private javax.swing.JButton ExitButton1;
    private javax.swing.JButton OkButton;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField screenAddress;
    private javax.swing.JComboBox screenCreditCardBank;
    private javax.swing.JTextField screenCreditCardCvv;
    private javax.swing.JTextField screenCreditCardNumber;
    private javax.swing.JTextField screenCreditCardOwnerName;
    private javax.swing.JTextField screenEmail;
    private javax.swing.JTextField screenFirstName;
    private javax.swing.JTextField screenLastName;
    private javax.swing.JTextField screenPointsCardNumber;
    // End of variables declaration//GEN-END:variables
}